package com.viewpagertext.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.viewpagertext.R;
import com.viewpagertext.constructor.Song;

import net.sourceforge.pinyin4j.PinyinHelper;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class LocalSongRecAdapter extends RecyclerView.Adapter<LocalSongRecAdapter.MyViewHolder> implements View.OnClickListener {

    private Context context;
    private List<Song> list;

    public LocalSongRecAdapter(Context context, List<Song> list) {
        this.context = context;
        Collections.sort(list, new Comparator<Song>() {
            @Override
            public int compare(Song song, Song t1) {
                return toPinyinString(song.song).compareTo(toPinyinString(t1.song));
            }
        });
        this.list = list;
    }


    private String toPinyinString(String string) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            if (isChineseCharacter(c)) {
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
                if (pinyinArray != null && pinyinArray.length > 0) {
                    sb.append(pinyinArray[0]); // 使用第一个拼音
                } else {
                    sb.append(c); // 如果不是汉字，直接添加到结果中
                }
            } else {
                sb.append(c); // 如果不是汉字，直接添加到结果中
            }
        }
        return sb.toString();
    }

    private boolean isChineseCharacter(char c) {
        Character.UnicodeBlock block = Character.UnicodeBlock.of(c);
        return (block == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || block == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || block == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || block == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || block == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MyViewHolder myViewHodler = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.load_item, parent, false));
        return myViewHodler;

    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        Collections.sort(list, new Comparator<Song>() {
            @Override
            public int compare(Song song, Song t1) {
                return toPinyinString(song.song).compareTo(toPinyinString(t1.song));
            }
        });

        holder.t_position.setText(position + 1 + "");
        holder.t_song.setText(list.get(position).song);
        holder.t_singer.setText(list.get(position).singer);

        holder.itemView.setTag(position);
        holder.t_position.setTag(position);
        holder.t_song.setTag(position);
        holder.t_singer.setTag(position);
        holder.localmenu.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView t_position, t_song, t_singer;
        private ImageView localmenu;

        public MyViewHolder(View itemView) {
            super(itemView);
            t_position = itemView.findViewById(R.id.t_postion);
            t_song = itemView.findViewById(R.id.t_song);
            t_singer = itemView.findViewById(R.id.t_singer);
            localmenu = itemView.findViewById(R.id.localmenu);

            itemView.setOnClickListener(LocalSongRecAdapter.this);
            localmenu.setOnClickListener(LocalSongRecAdapter.this);
        }
    }

    //=======================以下为item中的button控件点击事件处理===================================

    //item里面有多个控件可以点击（item+item内部控件）
    public enum ViewName {
        ITEM,
        PRACTISE
    }

    //自定义一个回调接口来实现Click和LongClick事件
    public interface OnItemClickListener {
        void onItemClick(View v, ViewName viewName, int position);

        void onItemLongClick(View v);
    }

    private OnItemClickListener mOnItemClickListener;//声明自定义的接口

    //定义方法并传给外面的使用者
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();      //getTag()获取数据
        if (mOnItemClickListener != null) {
            switch (v.getId()) {
                case R.id.mylist:
                    mOnItemClickListener.onItemClick(v, ViewName.PRACTISE, position);
                    break;
                default:
                    mOnItemClickListener.onItemClick(v, ViewName.ITEM, position);
                    break;
            }
        }
    }
}
